package com.skyworth.web.admin.company.controller;

import com.skyworth.web.admin.company.domain.CompanyModel;
import com.skyworth.web.admin.company.form.CompanyForm;
import com.skyworth.web.admin.company.service.CompanyService;
import com.skyworth.web.admin.store.model.StoreModel;
import com.skyworth.web.admin.store.service.StoreService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.MenuCss;
import com.skyworth.web.common.form.Paginator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by skyworth on 2016/11/10.
 */
@Controller
@RequestMapping("/admin/company")
public class CompanyController extends BaseController {

	Logger LOG = LoggerFactory.getLogger(CompanyController.class);

	@Autowired
	private StoreService storeService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private MenuCss menuCss;

	@RequestMapping("")
	public String index(Model model) {
		UserModel userModel = currentUser();
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCompany = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		CompanyModel companyModel = companyService.getCompany(userModel.getCompanyId());
		model.addAttribute("company",companyModel);
		return "admin/company/mycompany";
	}

	@RequestMapping(value = {"{id}"}, method = RequestMethod.GET)
	public String edit(@PathVariable("id") long id, Model model) {
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCompany = "hover";
		CompanyModel companyModel = companyService.getCompany(id);
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("company", companyModel);
		return "admin/company/company_edit";
	}

	@RequestMapping("/substore")
	public String index(Paginator paginator , Model model) {
		UserModel userModel = currentUser();
		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouCompanyStore = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		Page<StoreModel> page = storeService.storePageByCompany(userModel.getCompanyId(), paginator.pageRequest());
		paginator.feedToModel(page,model);
		return "admin/company/substores";
	}


	@RequestMapping(value = {"{id}"}, method = RequestMethod.PUT)
	@ResponseBody
	public Map<String, Object> update(@PathVariable("id") long id,
									  @ModelAttribute CompanyForm companyForm,
									  Model model) {
		Map<String, Object> map = new HashMap<String, Object>();
		companyForm.setId(id);
		CompanyModel companyModel = companyService.updateCompany(companyForm);

		model.addAttribute("company", companyModel);
		UserModel userModel = currentUser();
		map.put("valid", true);

		map.put("url", "/admin/company");

		return map;
	}
}
